  import { useDispatch } from "react-redux";
import store from "@/store";
import { useMemo } from "react";
import { renderMenuItem } from "@/router";
import { matchPath, useLocation } from "react-router-dom";

// 封装useDispatch
type TDispatch = typeof store.dispatch; // 限制类型
export const useAppDispatch = () => useDispatch<TDispatch>() // 



export const useAppTilte = () => {
  const location = useLocation();
  return useMemo(() => {
    const path = "/" + location.pathname.split("/")[1];
    // 根据路由path找到路由信息
    const info = renderMenuItem().find((v: any) => v.path === path);
    // 找到一级标题
    const title = info.label;
    // 找二级标题
    let subTitle;
    if (info.children) {
      // 这里的算法 有问题 二级标题会进入不了这个过程  医院详情的标题会是undefined
      // const subInfo = info.children.find((v: any) => v.path === location.pathname);

      /*    
      matchPath:是一个方法，可以查看请求地址与路由地址是否匹配。第一个参数是路由设置的path,第二个参数是浏览器地址
      如果不匹配返回的是null,如果匹配返回的是匹配相关信息（对象）  */
      // console.log(path);
      
      const subInfo = info.children.find((v:any)=>matchPath(v.path,location.pathname));
      // console.log(subInfo);
      

      if (subInfo) subTitle = subInfo.label;
    }
    return {
      title,
      subTitle
    }
  }, [location.pathname])
}

